﻿package group.lv2;

import group.receiver.MessageReceiver;
import group.sender.MessageSender;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(55533);
        System.out.println("server等待连接");
        while (true){
            final Socket socket = server.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (socket != null){
                            MessageReceiver receiver = new MessageReceiver(socket);
                            System.out.println("client: " + receiver.receiveMessage());

                            MessageSender sender = new MessageSender(socket);
                            sender.sendMessage("hello,client");

                            sender.close();
                            receiver.close();
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    }
}